package com.decent.ejfadmin.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * describe:
 * base64转换为图片
 *
 * @author tangdl
 * @date 2019/04/13
 */
@Slf4j
public final class BaseToImage {
    /**
     * base64字符串转化成图片
     *
     * @param imgStr
     * @return 如果返回null 表示没有保存成功图片
     */
    public static String generateImage(String imgStr) {
        if (StringUtils.isBlank(imgStr)) {
            return null;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                //调整异常数据
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            fileExistsCreate();

            //生成jpeg图片
            String imgFilePath = getImagePath();
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            IOUtils.closeQuietly(out);
            return imgFilePath;
        } catch (Exception e) {
            log.warn("base64转换为图片发生异常[{}]", e.getLocalizedMessage(), e);
            return null;
        }
    }

    private static String fileExistsCreate() {
        String path = getPath();
        File file = new File(path);
        if (file.exists()) {
            return path;
        }
        try {
            file.mkdir();
        } catch (Exception e) {
            log.warn("创建文件发生异常[{}]", e.getLocalizedMessage(), e);
        }
        return path;
    }


    /**
     * 闲鱼图片二进制转换
     *
     * @param imgStr
     * @param imgName
     * @return
     */
    public static String generateXianyuImage(String imgStr, String imgName) {
        if (StringUtils.isBlank(imgStr)) {
            return null;
        }
        OutputStream out = null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                //调整异常数据
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            fileExistsCreate();
            //生成jpeg图片
            String imgFilePath = getXianyuImgPath(imgName);
            out = new FileOutputStream(imgFilePath);
            out.write(b);
            return imgFilePath;
        } catch (Exception e) {
            log.warn("[闲鱼]base64转换为图片发生异常[{}]", e.getLocalizedMessage(), e);
            return null;
        } finally {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                log.warn("[闲鱼]IOException异常[{}]", e.getLocalizedMessage(), e);
                return null;
            }
        }
    }

    /**
     * 返回TXT路径
     *
     * @return
     */
    private final static String getXianyuImgPath(String picName) {
        return System.getProperty("user.dir") + System.getProperty("file.separator") + "logs" + System.getProperty("file.separator") + picName + ".jpg";
    }

    /**
     * 返回图片路径
     *
     * @return
     */
    private final static String getImagePath() {
        if (judgeOs()) {
            return System.getProperty("user.dir") + System.getProperty("file.separator") + "logs" + System.getProperty("file.separator") + System.nanoTime() + ".jpg";
        }
        return System.getProperty("user.dir") + System.getProperty("file.separator") + "logs" + System.getProperty("file.separator") + System.nanoTime() + ".jpg";
    }

    /**
     * 返回文件路径
     *
     * @return
     */
    private final static String getPath() {
        if (judgeOs()) {
            return System.getProperty("user.dir") + System.getProperty("file.separator") + "logs";
        }
        return System.getProperty("user.dir") + System.getProperty("file.separator") + "logs";
    }

    /**
     * 判定操作系统
     *
     * @return
     */
    private final static boolean judgeOs() {
        String os = System.getProperty("os.name");
        String operationSystem = "win";
        if (StringUtils.contains(StringUtils.lowerCase(os), operationSystem)) {
            return true;
        }
        return false;
    }
}
